package com.gaoyang.task;

import com.gaoyang.util.HttpUtils4Black;
import com.gaoyang.util.MailUtil;
import com.gaoyang.util.SslUtils;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.StringReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ScheduledTasks4Zhongxin {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

//    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() throws Exception{
//        String timeStart = DateUtils.getTodayStr() + " 09:59:56";
//        String timeEnd = DateUtils.getTodayStr() + " 10:00:01";
//        if (DateUtils.compare_date(new Date(), timeStart) == 1
//                && DateUtils.compare_date(new Date(), timeEnd) == -1) {
////        if (true) {
//            final ExecutorService pool = Executors.newFixedThreadPool(10);
//            Callable callable = new ZhongxinThread();
//            Future future = pool.submit(callable);
//            System.out.println("中信 现在时间：" + dateFormat.format(new Date()) + ">>" + future.get());
//        } else {
//            System.out.println("中信 现在时间：" + dateFormat.format(new Date()) + " " + System.currentTimeMillis());
//        }
        URL realUrl = null;
        try {
            String url = "https://dknp.e-pointchina.com.cn" + "/shop_citic_wap/cloudDataService.do?timestamp=" + System.currentTimeMillis();
            realUrl = new URL(url);
            if("https".equalsIgnoreCase(realUrl.getProtocol())){
                SslUtils.ignoreSsl();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("serviceType", "com.ebuy.shop.web.service.ItemSearchService");
        paramMap.put("serviceMethod", "searchAllItems2");
        paramMap.put("tag", "%E5%8C%97%E4%BA%AC%E5%B8%82");
        paramMap.put("offset", "0");
        paramMap.put("page_size", "40");
        String result = HttpUtils4Black.postUrl4Zhongxin(realUrl.toString(), paramMap);

        try {
            //1.创建SAXBuilder对象
            SAXBuilder saxBuilder = new SAXBuilder();
            org.jdom.Document document = saxBuilder.build(new StringReader(result));
            //4.获取根节点
            Element rootElement = document.getRootElement();
            Element dataList = rootElement.getChild("dataList");
            //5.获取子节点
            List<Element> children = dataList.getChildren();
            StringBuffer sb = new StringBuffer();
            boolean isSendMail = false;
            for (Element child : children) {
                List<Attribute> attributes = child.getAttributes();
                //打印属性
                for (Attribute attr : attributes) {
                    System.out.println(attr.getName()+":"+attr.getValue());
                }
                List<Element> childrenList = child.getChildren();
                System.out.println("======获取子节点-start======");
                String name = "";
                String value = "0";
                for (Element o : childrenList) {
                    if (o.getName().equals("name")) {
                        name = o.getValue();
                    }
                    if (name.contains("京东50") || name.contains("必胜客") || name.contains("稻香村")) {
                        if (o.getName().equals("stockNum")) {
                            value = o.getValue();
                        }
                    }
                    System.out.println("节点名:"+o.getName()+"---"+"节点值:"+o.getValue());
                }
                if (Integer.parseInt(value) > 0) {
                    isSendMail = true;
                    sb.append(name + ":" + value);
                }
                System.out.println("======获取子节点-end======" + sb.toString());
            }
            if (isSendMail) {
                MailUtil.sendMail("中信9积分提醒", sb.toString());
                MailUtil.sendMail2("中信9积分提醒", sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {


    }
}